package com.example.delivery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.delivery.pojo.User;
import com.example.delivery.service.UserService;
import com.example.delivery.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
* @author huber
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-03-18 13:29:36
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean registerUser(User user) {
        // 数据校验
        if (user.getUserName() == null || user.getPassword() == null) {
            return false;
        }
        // 1、先判断用户是否存在
        // 根据用户名查询用户
        String userName = user.getUserName();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", userName);
        User user1 = userMapper.selectOne(wrapper);
        if(user1!=null) { //用户已存在，注册失败
            return false;
        }
        user.setNickName(userName);
        user.setCreateTime(new Date());
        user.setBalance(BigDecimal.valueOf(1000000));
        userMapper.insert(user);
        return true;
    }


    @Override
    public boolean loginUser(String username, String password) {
        // 查询用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", username).eq("password",password);
        User user = userMapper.selectOne(wrapper);
        if(user == null) {
            return false;
        }
        return true;
    }

    @Override
    public User findByUserName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", username);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }
}




